//
//  BaseNavController.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "BaseNavController.h"

@interface BaseNavController ()<UIGestureRecognizerDelegate>
@property(nonatomic, strong) NSMutableArray *blackList;

@end

@implementation BaseNavController

#pragma mark - Lazy load
- (NSMutableArray *)blackList {
    if (!_blackList) {
        _blackList = [NSMutableArray array];
    }
    return _blackList;
}


#pragma mark - Public
- (void)addFullScreenPopBlackListItem:(UIViewController *)viewController {
    if (!viewController) {
        return ;
    }
    [self.blackList addObject:viewController];
}

- (void)removeFromFullScreenPopBlackList:(UIViewController *)viewController {
    for (UIViewController *vc in self.blackList) {
        if (vc == viewController) {
            [self.blackList removeObject:vc];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
       id target = self.interactivePopGestureRecognizer.delegate;
       SEL handler = NSSelectorFromString(@"handleNavigationTransition:");
       //  获取添加系统边缘触发手势的View
       UIView *targetView = self.interactivePopGestureRecognizer.view;
       
       //  创建pan手势 作用范围是全屏
       UIPanGestureRecognizer * fullScreenGes = [[UIPanGestureRecognizer alloc]initWithTarget:target action:handler];
       fullScreenGes.delegate = self;
       [targetView addGestureRecognizer:fullScreenGes];
       
       // 关闭边缘触发手势 防止和原有边缘手势冲突
       [self.interactivePopGestureRecognizer setEnabled:NO];
        self.navigationBar.hidden = YES;
    
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance * bar = [UINavigationBarAppearance new];
        bar.backgroundColor = UIColor.whiteColor;
        bar.backgroundEffect = nil;
        self.navigationBar.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
        self.navigationBar.scrollEdgeAppearance = bar;
        self.navigationBar.standardAppearance = bar;
        } else {
            self.navigationBar.barStyle = UIBarStyleDefault;
        }
}

#pragma mark - UIGestureRecognizerDelegate
//  防止导航控制器只有一个rootViewcontroller时触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    
    
    // 根据具体控制器对象决定是否开启全屏右滑返回
    for (UIViewController *viewController in self.blackList) {
        if ([self topViewController] == viewController) {
            return NO;
        }
    }
    
    
    if ([[self valueForKey:@"_isTransitioning"] boolValue]) {
        return NO;
    }
    
    
    // 解决右滑和UITableView左滑删除的冲突
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    if (translation.x <= 0) {
        return NO;
    }

    return self.childViewControllers.count == 1 ? NO : YES;

}


//push的时候隐藏tabbar
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.viewControllers.count > 0) {
           // 当前导航栏, 只有第一个viewController push的时候设置隐藏
           if (self.viewControllers.count == 1) {
               viewController.hidesBottomBarWhenPushed = YES;
           }
       } else {
           viewController.hidesBottomBarWhenPushed = NO;
       }

    [super pushViewController:viewController animated:animated];
}



@end
